/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from 'antd';
import { isLegalData } from '@/utils/utils';
import { getExportOrderConfig } from '@/services/order';
import { getOrderList, downloadOrderList, siteOperator } from '../services/api';

const noop = () => {};
export default {
  namespace: 'order',
  state: {
    data: [],
    list: [],
    pagination: null,
    orderDetailList: [],
    exportOrderConfig: {}, // 对账单导出界面信息
  },

  effects: {
    *getOrderList({ payload }, { call, put }) {
      const response = yield call(getOrderList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const { result, total, page, size } = data;
      if (code == 0 && data) {
        yield put({
          type: 'save',
          payload: {
            list: isLegalData(result, []),
            pagination: {
              total,
              pageSize: size,
              current: page,
            },
          },
        });
      } else {
        yield put({
          type: 'save',
          payload: {
            list: isLegalData(result, []),
            pagination: {
              total,
              pageSize: size,
              current: page,
            },
          },
        });
        message.error(msg);
      }
    },
    *downloadOrderList({ payload }, { call }) {
      const response = yield call(downloadOrderList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0 && data) {
        message.success('导出成功');
      } else {
        message.error(msg);
      }
    },
    *siteOperator({ payload, then = noop }, { call, put }) {
      const response = yield call(siteOperator, payload);
      const { code, data } = response;
      if (code == 0 && data) {
        yield put({
          type: 'save',
          payload: {
            data: isLegalData(data, []),
          },
        });
      }
      then(data);
    },
    *getExportOrderConfig({ payload }, { call, put }) {
      const response = yield call(getExportOrderConfig, payload);
      const { code, data } = response;
      if (code == 0 && data) {
        yield put({
          type: 'save',
          payload: {
            exportOrderConfig: isLegalData(data, {}),
          },
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
